package com.weikuang.fareast.adapter;

import android.os.Build;
import android.support.annotation.RequiresApi;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

import com.weikuang.fareast.R;
import com.weikuang.fareast.bean.DistributionSendBean;

import cn.dlc.commonlibrary.ui.adapter.BaseRecyclerAdapter;


/**
 * 通道门
 */
public class DistributionSendAdapter extends BaseRecyclerAdapter<DistributionSendBean> {
    @Override
    public int getItemLayoutId(int viewType) {
        return R.layout.item_distribution_center;
    }

    public DistributionSendAdapter() {

    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onBindViewHolder(CommonHolder holder, int position) {
        DistributionSendBean bean = getItem(position);

        holder.getText(R.id.hospital).setText(bean.hospital);
        holder.getText(R.id.offic_number).setText(bean.offic_number);
        holder.getText(R.id.specification).setText(bean.specification);
        holder.getText(R.id.offic_name).setText(bean.offic_name);

        EditText mExceptionEditText = holder.getView(R.id.ed_num);

        if (mExceptionEditText.getTag(R.id.ed_num) instanceof TextWatcher){
            mExceptionEditText.removeTextChangedListener(((TextWatcher)mExceptionEditText.getTag(R.id.ed_num)));
        }
        //移除了TextWatcher事件后设置item对应的文本
        if (bean.num == 0) {
            mExceptionEditText.setText("");
        }else {
            mExceptionEditText.setText(bean.num + "");
        }
        mExceptionEditText.setSelection(mExceptionEditText.getText().length());
        TextWatcher textWatcher=new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() > 0) {
                    bean.num = Integer.valueOf(s.toString());
                }
            }
        };
        //设置tag为TextWatcher
        mExceptionEditText.addTextChangedListener(textWatcher);
        mExceptionEditText.setTag(R.id.ed_num,textWatcher);

    }
}
